package com.ruoyi.project.websitefilemanage.controller;

import java.util.List;

import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.websitefilemanage.domain.WebsiteFileManage;
import com.ruoyi.project.websitefilemanage.service.IWebsiteFileManageService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 网页文件管理Controller
 * 
 * @author lvyongwei
 * @date 2021-04-26
 */
@Controller
@RequestMapping("/websitefilemanage/filemanage")
public class WebsiteFileManageController extends BaseController
{
    private String prefix = "system/websitefilemanage";

    @Autowired
    private IWebsiteFileManageService websiteFileManageService;

    @RequiresPermissions("websitefilemanage:filemanage:view")
    @GetMapping()
    public String filemanage()
    {
        return prefix + "/filemanage";
    }

    /**
     * 查询网页文件管理列表
     */
    @RequiresPermissions("websitefilemanage:filemanage:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WebsiteFileManage websiteFileManage)
    {
        startPage();
        List<WebsiteFileManage> list = websiteFileManageService.selectWebsiteFileManageList(websiteFileManage);
        return getDataTable(list);
    }

    /**
     * 导出网页文件管理列表
     */
    @RequiresPermissions("websitefilemanage:filemanage:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WebsiteFileManage websiteFileManage)
    {
        List<WebsiteFileManage> list = websiteFileManageService.selectWebsiteFileManageList(websiteFileManage);
        ExcelUtil<WebsiteFileManage> util = new ExcelUtil<WebsiteFileManage>(WebsiteFileManage.class);
        return util.exportExcel(list, "filemanage");
    }

    /**
     * 新增网页文件管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存网页文件管理
     */
    @RequiresPermissions("websitefilemanage:filemanage:add")
    @Log(title = "网页文件管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WebsiteFileManage websiteFileManage, MultipartFile file)
    {
        // 上传文件路径
        String filePath = RuoYiConfig.getUploadPath();
        /**
         * 文件上传
         */
        if(StringUtils.isEmpty(file) || file==null){
            return AjaxResult.error("文件为空，请上传文件");
        }
        try{
            /**
             * 文件名称获取
             */
            String fileName = file.getOriginalFilename();
            String prefix = fileName.substring(0,fileName.lastIndexOf('.'));
            websiteFileManage.setWebsiteManageName(prefix);
            /**
             * 文件上传
             */
            String filename = FileUploadUtils.upload(filePath, file);
            websiteFileManage.setWebsiteManageSrc(filename);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        return toAjax(websiteFileManageService.insertWebsiteFileManage(websiteFileManage));
    }

    /**
     * 修改网页文件管理
     */
    @GetMapping("/edit/{websiteManageId}")
    public String edit(@PathVariable("websiteManageId") Long websiteManageId, ModelMap mmap)
    {
        WebsiteFileManage websiteFileManage = websiteFileManageService.selectWebsiteFileManageById(websiteManageId);
        mmap.put("websiteFileManage", websiteFileManage);
        return prefix + "/edit";
    }

    /**
     * 修改保存网页文件管理
     */
    @RequiresPermissions("websitefilemanage:filemanage:edit")
    @Log(title = "网页文件管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WebsiteFileManage websiteFileManage,MultipartFile file)
    {
        //获取文件原地址
        String src = websiteFileManage.getWebsiteManageSrc();
        // 上传文件路径
        String filePath = RuoYiConfig.getUploadPath() + src;
        String replace = filePath.replaceAll("/profile/upload","");
        //删除旧文件
        try{
            FileUtils.deleteFile(replace);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        /**
         * 文件上传
         */
        try{
            /**
             * 文件名称获取
             */
            String filePath1 = RuoYiConfig.getUploadPath();
            String fileName = file.getOriginalFilename();
            String prefix = fileName.substring(0,fileName.lastIndexOf('.'));
            websiteFileManage.setWebsiteManageName(prefix);
            //上传
            String filename = FileUploadUtils.upload(filePath1, file);
            websiteFileManage.setWebsiteManageSrc(filename);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        return toAjax(websiteFileManageService.updateWebsiteFileManage(websiteFileManage));
    }

    /**
     * 删除网页文件管理
     */
    @RequiresPermissions("websitefilemanage:filemanage:remove")
    @Log(title = "网页文件管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        String[] array = ids.split(",");
        try{
            for(String id : array){
                //获取文件地址
                WebsiteFileManage websiteFileManage = websiteFileManageService.selectWebsiteFileManageById(Long.valueOf(id));
                //获取文件原地址
                String src = websiteFileManage.getWebsiteManageSrc();
                // 上传文件路径
                String filePath = RuoYiConfig.getUploadPath() + src;
                String replace = filePath.replaceAll("/profile/upload","");
                FileUtils.deleteFile(replace);
            }
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        return toAjax(websiteFileManageService.deleteWebsiteFileManageByIds(ids));
    }
}
